<template>
  <div>
    <div class="px-4" v-loading="loading">
      <handler-attach
        :query-id="queryId"
        :view-mode="viewMode"
        @getRelated="getRelatedData"
      ></handler-attach>
      <div class="text-center mt-5" v-if="+relatedData.item_status === 0 && !viewMode">
        <el-button
          class="btn-long"
          type="primary"
          @click="handlerRegister"
          :loading="submitLoading"
        >
          完成登记
        </el-button>
        <el-button class="btn-long" type="danger" @click="handleReject" :loading="submitLoading">
          拒绝
        </el-button>
      </div>
    </div>
  </div>
</template>

<script>
import HandlerAttach from '@/bigcredit/views/self/fast/item/components/HandlerAttach'
export default {
  components: {
    HandlerAttach,
  },
  props: {
    queryId: String,
    viewMode: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      loading: false,
      submitLoading: false,
      tableName: 'customer_related',
      relatedData: {},
    }
  },
  computed: {},
  watch: {},
  created() {},
  mounted() {},
  methods: {
    getRelatedData(data) {
      this.relatedData = { ...data }
    },
    handlerRegister() {
      this.submitLoading = true
      this.$api
        .updateData(this.$serviceName, this.tableName, this.queryId, {
          item_status: 1,
          item_date: this.$utils.dateFormat(new Date()),
          project_res_status: 2,
          contract_approval_status: 3,
          loan_approval_status: 2,
        })
        .then(() => {
          this.$message.success('登记成功！')
          this.$emit('success', true)
          this.$emit('close', true)
        })
        .catch(err => {
          this.$message.error(err?.data?.error_description || '登记失败，请重试！')
        })
        .finally(() => {
          this.submitLoading = false
        })
    },
    handleReject() {
      const data = {
        item_status: 2,
        confuse_status: 3,
        confuse_time: this.$utils.dateFormat(new Date()),
        confuse_person: this.user.realName,
      }
      this.$prompt('请输入拒绝说明', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        inputValidator: value => (!value ? '请输入拒绝说明' : true),
      }).then(({ value }) => {
        data.confuse_des = value
        this.$api
          .updateData(this.$serviceName, 'customer_related', this.queryId, data)
          .then(() => {
            this.$message.success('已拒绝！')
            this.$emit('success', true)
            this.$emit('close', true)
          })
          .catch(err => {
            this.$message.error(err?.data?.error_description || '登记失败，请重试！')
          })
      })
    },
  },
}
</script>

<style lang="scss" scoped>
.attach-title {
  font-size: 1.1rem;
  font-weight: bold;
}
.attach-tag-label {
  width: 180px;
  width: var(--tag-balel-width, 180px);
  padding-top: 0.25rem;
  padding-left: 0.5rem;
}
</style>
